<template>
  <div class="topbar">
    <div class="wrap topbarwrap">
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <!-- 右侧用户信息 -->
      <ul class="right">
        <!--     -->
        <li>
          <img :src="userInfo.headImg" alt="" class="avater" />
          用户名：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li
          class="login-btn"
          id="wahaha"
          ref="wahaha2"
          v-else
          @click="changeIsShowLoginModal(true)"
        >
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapState, mapMutations, mapActions } from "vuex";
import { weixinLoginAPI, UserProfilesAPI } from "@/request/api.js";

export default {
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      cartTotal: (state) => state.userInfo.cartTotal,
      userInfo: (state) => state.userInfo.userInfo,
    }),
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsLogined: "loginStatus/changeIsLogined",
      changeUserInfo: "userInfo/changeUserInfo",
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncShowToast: "showToast/asyncShowToast",
      asyncUserInfo: "userInfo/asyncUserInfo",
    }),
  },

  async created() {
    console.log("topbar-creaed");
    // console.log("code", this.$route.query.code);
    this.$router.onReady(async () => {
      let path = this.$route.query.code;
      if (path) {
        let res = await weixinLoginAPI({
          code: path,
        });
        //    console.log("********************res", res);
        if (res.code == 0) {
          this.asyncShowToast({
            isShow: true,
            title: res.message,
            type: "success",
          });
          //设置token
          localStorage.setItem("x-auth-token", res["x-auth-token"]);
          //设置登录状态
          this.changeIsLogined(true);
          //修改路由地址
          this.$router.push(this.$route.path);
          this.asyncUserInfo();
        } else if (res.code == 400) {
          this.asyncShowToast({
            isShow: true,
            title: res.message,
            type: "danger",
          });
          this.changeIsShowLoginModal(true);
        } else if (res.code == 407) {
          this.asyncShowToast({
            isShow: true,
            title: res.message,
            type: "danger",
          });
          //重新请求该地址
          this.$router.push(this.$route.path);
          //打开登录框
          this.changeIsShowLoginModal(true);
          console.log(res);
          console.log(res.uuid);

          //储存uuid
          localStorage.setItem("uuid", res.uuid);
        }
      } else {
        //判断是否有token
        let token = localStorage.getItem("x-auth-token");
        this.changeIsLogined(Boolean(token));
        if (token) {
          this.asyncUserInfo();
        } else {
          this.initUserInfo();
        }
      }
    });
  },

  // watch: {
  //   "$route.path": {
  //     handler(nweVal, oldVal) {
  //       console.log("地址改变了", nweVal, oldVal);
  //       this.changeIsLogined(Boolean(localStorage.getItem("x-auth-token")));
  //     },
  //   },
  // },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  color: #fffefe;
  .topbarwrap {
    display: flex;
    justify-content: space-between;
    text-align: center;
    .left {
      width: 155px;
      height: 15px;
      font-size: 14px;
    }
    .right {
      font-size: 14px;
      li {
        float: left;
        margin-left: 20px;
        &:hover {
          cursor: pointer;
        }
        &.login-btn {
          width: 124px;
          background-color: #0a328e;
        }
      }
    }
    .avater {
      width: 23px;
      height: 22px;
      border-radius: 50%;
      vertical-align: middle;
    }
  }
}

.cart-btn {
  width: 124px;
  height: 40px;
  background: #0a328e;
  text-align: center;
  img {
    width: 20px;
    height: 19px;
    vertical-align: middle;
  }
  span {
    margin: 0 8px;
  }
  strong {
    width: 22px;
    height: 22px;
    line-height: 22px;
    display: inline-block;
    border-radius: 50%;
    background-color: #fd604d;
  }
}
</style>